<?php
class Adm_Form_User extends Zend_Form
{
	protected $_exclude;
	protected $_passwordRequired = true ;
	private $_userTypes;
	private $_userSexs;
	private $_cities;
	
	public function __construct($options=null) {
		
		if (isset($options['passwordRequired'])){
			$this->_passwordRequired = (bool) $options['passwordRequired'];
			unset($options['passwordRequired']);
		}
		
		if (isset($options['exclude'])){
			$this->_exclude = $options['exclude'];
			unset($options['exclude']);
		}		
		
		$this->_userTypes= $options['userTypes'];
		unset($options['userTypes']);
		
		$this->_userSexs= $options['userSexs'];
		unset($options['userSexs']);
		
		$this->_cities= $options['cities'];
		unset($options['cities']);
		
		parent::__construct($options);
	}
	
	
	
	public function init()
	{				
	
		$this->setMethod('post');
		
		$email  = new Zend_Form_Element_Text('email');
		$email->setLabel('email');
		$email->setRequired(true);
		$email->setDescription('emailDesc');
		$email->addValidator(new Zend_Validate_EmailAddress());
		
		if ( $this->_exclude ){
			$email->addValidator(new Zend_Validate_Db_NoRecordExists(
				'users', 
				'email', 
				array(
					'field' => 'email', 
					'value' => $this->_exclude
				)
			));
			
		} else{
			$email->addValidator(new Zend_Validate_Db_NoRecordExists('users', 'email'));
		}
		
		$this->addElement($email);
		
			
		$password = new Zend_Form_Element_Password('password');
		$password->setLabel('Clave');
		
		$password->setRequired( $this->_passwordRequired );
		
		$password->addFilter('StripTags');
	    $password->addFilter('StringTrim');
		
//	    $clave->addValidator('Alnum');
//		$password->addValidator(new Zend_Validate_StringLength(5, 20));
		$password->addValidator( new My_Validate_IdenticalField ('password_confirm', 'Confirm Password'));
		$this->addElement($password);
		
		$password_confirm = new Zend_Form_Element_Password('password_confirm');
		$password_confirm->setLabel('repetir clave');
		$this->addElement($password_confirm);
		
				
		$name = new Zend_Form_Element_Text('name');
		$name->setLabel('Nombre');
		$name->setRequired(false);
//		$nombre->setDescription('userNameDesc');
		$name->addValidator(new Zend_Validate_Alnum( true ));
//		$name->addValidator(new Zend_Validate_StringLength(2, 45));		
				
		$this->addElement($name);	

		$surname = new Zend_Form_Element_Text('surname');
		$surname->setLabel('Apellido');
		$surname->setRequired(false);
//		$surname->setDescription('userNameDesc');
		$surname->addValidator(new Zend_Validate_Alnum( true ));
//		$surname->addValidator(new Zend_Validate_StringLength(2, 45));		
				
		$this->addElement($surname);	
		
		$birthdate = new Zend_Form_Element_Text('birthdate');
		$birthdate->setLabel('Fecha de nacimiento');
		$birthdate->setRequired(false);
//		$surname->setDescription('userNameDesc');
//		$surname->addValidator(new Zend_Validate_Alnum( true ));
//		$surname->addValidator(new Zend_Validate_StringLength(2, 45));		
				
		$this->addElement($birthdate);
		
		$type = new Adm_Form_Element_UserType('type', $this->_userTypes);
		$type->setLabel('Tipo de usuario');
		$type->setRequired(true);
		
		$this->addElement($type);
		
		$sex = new Adm_Form_Element_Sex('sex', $this->_userSexs);
		$sex->setLabel('Sexo');
		$sex->setRequired(true);
		
		$city = new Adm_Form_Element_City('city', $this->_cities);
		$city->setLabel('Ciudad');
		$city->setRequired(true);
		
		$this->addElement($sex);
		
		
		$submit = new Zend_Form_Element_Submit('submit');
		$submit->setLabel('Enviar');
		$this->addElement($submit);
	
	}
	
	public function loadDefaultDecorators()	{
		
		$elements = $this->getElements();
		unset($elements['submit']);
		
		$this->addDecorator('FormElements')
			->addDecorator('HtmlTag', 	array('tag' => 'div', 'class' => 'zend_form'))
			->addDecorator('Form')
			->setElementDecorators(array(
				array('ViewHelper'),
				array('Errors', array('placement' => 'APPEND')),
				array('Description', 	array('tag' => 'em', 'class' => 'element-description')),
				array('Label', 			array('separator' => ' ', 'placement' => 'PREPEND')),
				array('HtmlTag', 		array('tag' => 'div', 'class' => 'el-group')),
				),
				array_keys($elements)
			);	

		$this->setElementDecorators(array(
				array('ViewHelper'),				
				array('HtmlTag', 		array('tag' => 'div', 'class' => 'submit-group')),
				),
				array('submit')
			);
	
	}



}